﻿#include <QGraphicsDropShadowEffect>
#include <QFile>
#include "CTaskCalendar.h"
#include "CGlobal.h"
#include "CTaskQueryDialog.h"
#include "CRBasicDataBase.h"

CTaskCalendar::CTaskCalendar(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	setStyleSheetFile();//设置界面样式表
	initConnect();      //初始化槽函数连接
	initData();         //初始化数据
	initUI();           //初始化界面
}

CTaskCalendar::~CTaskCalendar()
{
	if (nullptr != m_pYearSelected)
	{
		delete m_pYearSelected;
		m_pYearSelected = nullptr;
	}

	if (nullptr != m_pMonthSelected)
	{
		delete m_pMonthSelected;
		m_pMonthSelected = nullptr;
	}

	if (nullptr != m_pTaskBriefInfo)
	{
		delete m_pTaskBriefInfo;
		m_pTaskBriefInfo = nullptr;
	}
}

//设置场景Guid
void CTaskCalendar::setSceneGuid(QString sSceneGuid, QMap<int, QString> mapTimeType, CRBasicDataBase* pRobData)
{
	m_sSceneGuid = sSceneGuid;
	m_pTaskBriefInfo = new UTaskBriefInfo(m_sSceneGuid, mapTimeType, pRobData);
	m_pTaskBriefInfo->setParent(this, Qt::FramelessWindowHint | Qt::Window);
	m_pTaskBriefInfo->resize(200, 300);
	m_pCalendarWidget->setItemInfoWidget(m_pTaskBriefInfo);
	setCurrentData(QDate::currentDate());
}

/**
* @brief showEvent 页面显示时触发
* @param event 页面显示事件对象
*/
void CTaskCalendar::showEvent(QShowEvent * event)
{
	initDateSelect();
}

//设置界面样式表
void CTaskCalendar::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

//初始化槽函数连接
void CTaskCalendar::initConnect()
{
	connect(ui.pushButtonCurrentYear, SIGNAL(clicked()), this, SLOT(slotSelectYear()));
	connect(ui.pushButtonCurrentMonth, SIGNAL(clicked()), this, SLOT(slotSelectMonth()));
	connect(ui.pushButtonLastMonth, SIGNAL(clicked()), this, SLOT(slotLastMonth()));
	connect(ui.pushButtonNextMonth, SIGNAL(clicked()), this, SLOT(slotNextMonth()));
	connect(ui.pushButtonLastYear, SIGNAL(clicked()), this, SLOT(slotLastYear()));
	connect(ui.pushButtonNextYear, SIGNAL(clicked()), this, SLOT(slotNextYear()));
}

//初始化界面
void CTaskCalendar::initUI()
{
	QIcon icon(":/icon/Rob/Rob.ico");
	setWindowIcon(icon);
	setWindowTitle(A2T("日历选择"));

	setAttribute(Qt::WA_ShowModal, true);//模态

	QTextCharFormat temp_format;
	QColor color(60, 60, 60);
	temp_format.setForeground(color);

	m_pCalendarWidget->setWeekdayTextFormat(Qt::Saturday, temp_format);
	m_pCalendarWidget->setWeekdayTextFormat(Qt::Sunday, temp_format);
	m_pCalendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
	m_pCalendarWidget->setHorizontalHeaderFormat(QCalendarWidget::NoHorizontalHeader);
	m_pCalendarWidget->setMinimumSize(230, 190);
	m_pCalendarWidget->setFocusPolicy(Qt::NoFocus);
	m_pCalendarWidget->setFirstDayOfWeek(Qt::Sunday);
	m_pCalendarWidget->setLocale(QLocale::Chinese);
	m_pCalendarWidget->setNavigationBarVisible(false);

	ui.widget->setMinimumHeight(35);
	QGridLayout* layout = new QGridLayout;
	layout->addWidget(m_pCalendarWidget);
	layout->setContentsMargins(3, 3, 6, 3);
	ui.widget_3->setLayout(layout);

	setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
	setFixedSize(550, 600);
}

//初始化数据
void CTaskCalendar::initData()
{
	m_sSceneGuid = "";
	m_pMonthSelected = nullptr;
	m_pYearSelected = nullptr;
	m_pCalendarWidget = new CCustomCalendar(this);
	connect(m_pCalendarWidget, &QCalendarWidget::currentPageChanged, this, &CTaskCalendar::slotRefreshDate);
	connect(m_pCalendarWidget, &CCustomCalendar::sigSelectedDate, this, &CTaskCalendar::slotSelectedDate);
}

//设置当前的日期
void CTaskCalendar::setCurrentData(QDate date)
{
	QString strCurrentYear = QString("%1").arg(date.year()) +A2T("年");
	QString strCurrentMonth = QString("%1").arg(date.month()) +A2T("月");
	ui.pushButtonCurrentYear->setText(strCurrentYear);
	ui.pushButtonCurrentMonth->setText(strCurrentMonth);
	m_pCalendarWidget->setSelectedDate(date);
	m_pTaskBriefInfo->setDateInfo(date);
}

//获取当前的日期
QDate CTaskCalendar::getCurrentData()
{
	return m_pCalendarWidget->selectedDate();
}

//初始化年份和月份的下拉选择
void CTaskCalendar::initDateSelect()
{
	/*初始化选择年  前后2年*/
	m_pYearSelected = new QComboBox();
	m_pYearSelected->setParent(this);
	int nYear = QDate::currentDate().toString("yyyy").toInt();
	int nStartYear = nYear - 2;
	int nEndYear = nYear + 2;
	int nIndex = 0;
	int nCurrentIndex = 0;
	for (int i = nStartYear;i <= nEndYear;i++)
	{
		m_pYearSelected->addItem(QString::number(i), i);
		//判断是否是当前年
		if (i == nYear)
		{
			nCurrentIndex = nIndex;
		}
		nIndex++;
	}
	m_pYearSelected->setCurrentIndex(nCurrentIndex);
	connect(m_pYearSelected, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeCurrentYear(int)));

	/*初始化选择月*/
	m_pMonthSelected = new QComboBox();
	m_pMonthSelected->setParent(this);
	int nMonth = QDate::currentDate().toString("MM").toInt();
	nIndex = 0;
	nCurrentIndex = 0;
	for (int i = 1; i <= 12; i++)
	{
		m_pMonthSelected->addItem(QString::number(i), i);
		//判断是否是当前年
		if (i == nMonth)
		{
			nCurrentIndex = nIndex;
		}
		nIndex++;
	}
	m_pMonthSelected->setCurrentIndex(nCurrentIndex);
	connect(m_pMonthSelected, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeCurrentMonth(int)));
}

//选择年
void CTaskCalendar::slotSelectYear()
{
	//显示年份下拉框
	QPoint qLTPoint = ui.pushButtonCurrentYear->mapToParent(QPoint(0, 0));
	QPoint qYearPos = QPoint(qLTPoint.x() * 1.1, qLTPoint.y() + ui.pushButtonCurrentYear->height() * 1.3);
	m_pYearSelected->setFixedSize(ui.pushButtonCurrentYear->rect().width(), ui.pushButtonCurrentYear->rect().height());
	m_pYearSelected->move(qYearPos);
	m_pYearSelected->showPopup();
}

//切换当前年份
void CTaskCalendar::slotChangeCurrentYear(int nIndex)
{
	int nYear = m_pYearSelected->currentData().toInt();
	int nMonth = m_pMonthSelected->currentData().toInt();

	ui.pushButtonCurrentYear->setText(QString("%1").arg(nYear) + A2T("年"));
	QDate date;
	date.setDate(nYear, nMonth, 1);
	setCurrentData(date);
	m_pYearSelected->hidePopup();
}

//选择月
void CTaskCalendar::slotSelectMonth()
{
	//显示年份下拉框
	QPoint qLTPoint = ui.pushButtonCurrentMonth->mapToParent(QPoint(0, 0));
	QPoint qYearPos = QPoint(qLTPoint.x() * 1.08, qLTPoint.y() + ui.pushButtonCurrentMonth->height() * 1.3);
	m_pMonthSelected->setFixedSize(ui.pushButtonCurrentMonth->rect().width(), ui.pushButtonCurrentMonth->rect().height());
	m_pMonthSelected->move(qYearPos);
	m_pMonthSelected->showPopup();
}

//切换当前月份
void CTaskCalendar::slotChangeCurrentMonth(int nIndex)
{
	int nYear = m_pYearSelected->currentData().toInt();
	int nMonth = m_pMonthSelected->currentData().toInt();

	ui.pushButtonCurrentMonth->setText(QString("%1").arg(nMonth) + A2T("月"));
	QDate date;
	date.setDate(nYear, nMonth, 1);
	setCurrentData(date);
	m_pMonthSelected->hidePopup();
}

//切换为上一年
void CTaskCalendar::slotLastYear()
{
	ui.pushButtonCurrentYear->setText(QString("%1").arg(m_pCalendarWidget->yearShown() - 1) +A2T("月"));
	m_pCalendarWidget->showPreviousYear();
}

//切换为下一年
void CTaskCalendar::slotNextYear()
{
	ui.pushButtonCurrentYear->setText(QString("%1").arg(m_pCalendarWidget->yearShown() + 1) + A2T("年"));
	m_pCalendarWidget->showNextYear();
}

//切换为上一月
void CTaskCalendar::slotLastMonth()
{
	if (m_pCalendarWidget->monthShown() != 1)
	{
		ui.pushButtonCurrentMonth->setText(QString("%1").arg(m_pCalendarWidget->monthShown() - 1) + A2T("月"));
	}
	else
	{
		ui.pushButtonCurrentMonth->setText(QString("%1").arg(12) + A2T("月"));
	}

	m_pCalendarWidget->showPreviousMonth();
}

//切换为下一月
void CTaskCalendar::slotNextMonth()
{
	if (m_pCalendarWidget->monthShown() != 12)
	{
		ui.pushButtonCurrentMonth->setText(QString("%1").arg(m_pCalendarWidget->monthShown() + 1) + A2T("月"));
	}
	else
	{
		ui.pushButtonCurrentMonth->setText(QString("%1").arg(1) + A2T("月"));
	}

	m_pCalendarWidget->showNextMonth();
}

//刷新当前显示的日期
void CTaskCalendar::slotRefreshDate(int year, int month)
{
	QDate date;
	date.setDate(year, month, 1);
	setCurrentData(date);
}

//发送日期选择信号
void CTaskCalendar::slotSelectedDate(QDate date)
{
	hide();

	if (nullptr != m_pTaskBriefInfo)
	{
		m_pTaskBriefInfo->hide();
	}

	emit sigSetDate(date);
}

